package com.usian.tu.service.ipl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.media.dtos.WmNewsPassDto;
import com.usian.tu.config.FastDFSClientUtil;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.model.media.pojos.WmUser;

import com.usian.tu.mapper.AdWmNewsMapper;
import com.usian.tu.mapper.AdwemediMapper;
import com.usian.tu.mapper.WmNewsMaterialMapper;
import com.usian.tu.service.WmMaterialservice;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

@Service
public class   WmMaterialserviceaipl  extends ServiceImpl<AdwemediMapper, WmMaterial> implements WmMaterialservice {

    @Resource
     FastDFSClientUtil fastDFSClientUtil;

    @Resource
     WmNewsMaterialMapper materialMapper;

    @Resource
     AdWmNewsMapper wmNewsMapper;

    @Value("${fdfs.url}")
    String fdfsurl;


//    上传图片
    @Override
    public ResponseResult sc(MultipartFile multipartFile) {
        if (multipartFile==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"文件为空");
        }
        String s="";
        try {
             s = fastDFSClientUtil.uploadFile(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"操作失败");
        }

        WmMaterial wm=new WmMaterial();
        wm.setCreatedTime(new Date());
        wm.setIsCollection(0);
        wm.setType(0);
        WmUser user = WmThreadLocalUtils.getUser();
        wm.setUserId(user.getApUserId());
        wm.setUrl(fdfsurl+s);
        save(wm);
        return ResponseResult.okResult(wm);
    }

//    素材查询
    @Override
    public ResponseResult sucailist(WmMaterialDto dto) {
        if (dto==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam();

        LambdaQueryWrapper<WmMaterial> qw=new LambdaQueryWrapper<>();
        qw.eq(dto.getIsCollection()!=null,WmMaterial::getIsCollection,dto.getIsCollection());
        Page page=new Page(dto.getPage(),dto.getSize());

        IPage l=page(page,qw);

           ResponseResult result= new PageResponseResult(dto.getPage(),dto.getSize(),(int)l.getTotal());
            result.setData(l.getRecords());


        return result;
    }

//    删除
    @Override
    public ResponseResult del(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"不能删除为空");
        }

        WmMaterial byId = getById(id);
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"未查找成功");
        }
        LambdaQueryWrapper<WmNewsMaterial> qw=new LambdaQueryWrapper<>();
        qw.eq(id!=null,WmNewsMaterial::getMaterialId,id);
        Integer integer = materialMapper.selectCount(qw);
        if (integer>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"当前图片被引用");
        }
        String replace = byId.getUrl().replace(fdfsurl, "");
        try {
            fastDFSClientUtil.delFile(replace);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"删除失败");
        }

        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

//根据 id修改状态
    @Override
    public ResponseResult zt(Integer id,Integer uid) {
        if (uid==null ||id ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"操作失败");
        }
        WmMaterial byId = getById(uid);
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"操作失败");
        }

        byId.setIsCollection(id);
        updateById(byId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

//    文章查询
    @Override
    public ResponseResult wzlist(WmNewsPageReqDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"操作失败");
        }

        dto.checkParam();
        LambdaQueryWrapper<WmNews> qw=new LambdaQueryWrapper<>();
        qw.eq(dto.getStatus()!=null,WmNews::getStatus,dto.getStatus());
        qw.eq(dto.getChannelId()!=null,WmNews::getChannelId,dto.getChannelId());
        qw.like(dto.getKeyword()!=null,WmNews::getTitle,dto.getKeyword());
        qw.between(dto.getBeginPubDate()!=null && dto.getEndPubDate()!=null,WmNews::getPublishTime,dto.getBeginPubDate(),dto.getEndPubDate());
        Page page=new Page(dto.getPage(),dto.getSize());
        IPage l = wmNewsMapper.selectPage(page, qw);

        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),(int)l.getTotal());
        result.setData(l.getRecords());
        return result;
    }

    @Override
    public ResponseResult wzlisttu(NewsAuthDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"操作失败");
        }
        dto.checkParam();
        LambdaQueryWrapper<WmNews> qw=new LambdaQueryWrapper<>();
        qw.like(dto.getTitle()!=null,WmNews::getTitle,dto.getTitle());
        Page page=new Page(dto.getPage(),dto.getSize());
        IPage l = wmNewsMapper.selectPage(page, qw);

        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),(int)l.getTotal());
        result.setData(l.getRecords());
        return result;
    }

    @Override
    public ResponseResult meitihuixian(Integer id) {
        WmNews wmNews = wmNewsMapper.selectById(id);
        return ResponseResult.okResult(wmNews);
    }

    @Override
    public ResponseResult pass(WmNewsPassDto wmNewsPassDto) {
        if (wmNewsPassDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaUpdateWrapper<WmNews> qw=new LambdaUpdateWrapper<>();
        qw.eq(wmNewsPassDto.getId()!=null,WmNews::getId,wmNewsPassDto.getId());
        qw.set(wmNewsPassDto.getStatus()!=null,WmNews::getStatus,8);
        qw.set(WmNews::getReason,"审核通过,待发布");
        WmNews wmNews=new WmNews();
//        wmNews.setId(wmNewsPassDto.getId());
//        wmNews.setStatus(8);
        wmNewsMapper.update(wmNews,qw);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult fail(WmNewsPassDto wmNewsPassDto) {
        if (wmNewsPassDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaUpdateWrapper<WmNews> qw=new LambdaUpdateWrapper<>();
        qw.eq(wmNewsPassDto.getId()!=null,WmNews::getId,wmNewsPassDto.getId());
        qw.set(wmNewsPassDto.getStatus()!=null,WmNews::getStatus,2);
        qw.set(wmNewsPassDto.getMsg()!=null,WmNews::getReason,wmNewsPassDto.getMsg());
        WmNews wmNews=new WmNews();
//        wmNews.setId(wmNewsPassDto.getId());
//        wmNews.setStatus(8);
        wmNewsMapper.update(wmNews,qw);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
